Page({
  data: {
    videoSrc: 'http://47.94.9.70/firmware/abced.mp4', // 替换为你的视频地址
    targetTime: 5.1, // 要捕获帧的时间点（秒）
    reachedTarget: false,
  },

  onLoad() {
    this.videoContext = wx.createVideoContext('myVideo');
    this.videoDom = wx.createSelectorQuery().select('#myVideo').context(res => {
      this.videoDomCtx = res.context
    }).exec()
    wx.createSelectorQuery().selectAll('#myCanvas').node(res => {
      this.ctx1 = res[0].node.getContext('2d')
      const dpr = wx.getWindowInfo().pixelRatio
      console.log("ctx======>",this.ctx1)
      res[0].node.width = 300 * dpr
      res[0].node.height = 200 * dpr
    }).exec()
  },

  onReady() {

  },

  captureFrame() {
    const { targetTime } = this.data;
    this.setData({ reachedTarget: false });
    let t = targetTime + 1
    this.setData({
      targetTime: t
    })
    // 跳转到目标时间点
    this.videoContext.seek(t);
    this.videoContext.sendDanmu({text:'askwan',color:"red"})
    setTimeout(() => {
      this.drawFrameToCanvas()
    }, 100);
  },

  onTimeUpdate(e) {
    return
    const { targetTime, reachedTarget } = this.data;

    if (!reachedTarget && Math.abs(e.detail.currentTime - targetTime) < 0.1) { // 检查是否接近目标时间
      this.setData({ reachedTarget: true });
      this.drawFrameToCanvas();
    }
  },

  drawFrameToCanvas() {

    const dpr = wx.getWindowInfo().pixelRatio
    this.ctx1.drawImage(this.videoDomCtx, 0, 0, 300 * dpr, 200 * dpr);
    setTimeout(() => {
      
      return
      this.videoDom.context(res => {
        // console.log('select video', res,dpr)
        const video = res.context
        // wx.createSelectorQuery().selectAll('#myCanvas').node(res => {
        //   console.log('select canvas', res)
        //   const ctx1 = res[0].node.getContext('2d')
        //   res[0].node.width = 300 * dpr
        //   res[0].node.height = 200 * dpr
   
        //   ctx1.drawImage(video, 0, 0, 300 * dpr, 200 * dpr);
        // }).exec()
        this.ctx1.drawImage(video, 0, 0, 300 * dpr, 200 * dpr);
      }).exec()
    }, 0);
    return
  },
});